package com.bite.ThreadPool;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;

public class MyThreadPool {
    private BlockingQueue<Runnable> queue = new LinkedBlockingQueue<>();

    public void sumbit(Runnable runnable) throws InterruptedException {
        queue.put(runnable);
    }

    //n 代表有几个线程
    public MyThreadPool(int n) {
        for (int i = 0; i < n; i++) {
            Thread thread = new Thread(() -> {
               while(true) {
                   Runnable runnable = null;
                   try {
                       runnable = queue.take();
                       runnable.run();
                       TimeUnit.SECONDS.sleep(3);
                   } catch (InterruptedException e) {
                       throw new RuntimeException(e);
                   }
               }
            });
            thread.start();
        }
    }
}
